package com.lxy.demo.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "SetCookieServlet", urlPatterns = "/SetCookieServlet")
public class SetCookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //1.创建Cookie对象
        Cookie cookie = new Cookie("username","郭德纲");
        //2.设置cookie的路径，项目名
        //getContextPath()  获取项目路径
        cookie.setPath(request.getContextPath());
        //3.设置cookie的存活时间
        cookie.setMaxAge(60*30);
        //4.把cookie响应到浏览器
        response.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
